package com.ljsw.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.ljsw.adapter.NotCompletedInfoAdapter;
import com.ljsw.application.App;
import com.ljsw.dialog.TimeOut;
import com.ljsw.entity.pkentity.PKNotCompleteInfoEntity;
import com.ljsw.server.UpServer;
import com.ljsw.util.DialogManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

/*****
 * 明细信息详细信息
 */
public class NotCompletedInfoActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "NotCompletedInfoActivity";
    private TextView BiaoCaidNo;//  标牌明细
    private TextView user;
    private TextView TaskNo, nocompletetaskno;   ///任务编号

    private TextView BiaopaiNo;
    private TextView tvcount;// 合计
    private ListView nocompletenolistview;//  listview;
    private NotCompletedInfoAdapter notCompletedInfoAdapter;

    private List<PKNotCompleteInfoEntity> listinfoentitylist = new ArrayList<PKNotCompleteInfoEntity>();//内层盘库明细实体
    private Context context;
    private Handler handler;
    private TimeOut timeout;//超时连接
    private DialogManager dmanager;//弹出框管理类
    private String param = "";// 上页面参数
    private String showAllmongye = "";//  显示总钱数
    private ImageView acnotcomblack;//  返回
    private String Taskno;//任务编号
    private App app;
    private Button hbt_ql_ruku_update;// 更新

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_not_completed_info);
        app = (App) this.getApplication();

        param = getIntent().getStringExtra("PKNo");//   获取数据
//        notcompletin.putExtra("PKNo", unscanlist.get(position));
//        notcompletin.putExtra("TaskNo", PKNo);
        Taskno = getIntent().getStringExtra("TaskNo");
        InitView();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 0://登录成功后跳转 上门服务页面
                        user = user;
                        Log.d("login用户role------------", "");
                        notCompletedInfoAdapter = new NotCompletedInfoAdapter(listinfoentitylist, NotCompletedInfoActivity.this);
                        nocompletenolistview.setAdapter(notCompletedInfoAdapter);
                        tvcount.setText(showAllmongye);      //  显示钱数
                        break;
                    case 1://登录超时
                        break;
                    case 2://用户不存在
                        Toast.makeText(NotCompletedInfoActivity.this, "加载异常" , Toast.LENGTH_SHORT).show();
                        break;
                    case 3://异常加载，无法访问服务器
                        Toast.makeText(NotCompletedInfoActivity.this, "" + "加载异常" + App.getApp().fanhuiMsg, Toast.LENGTH_SHORT).show();
                        timeout.show();
                        timeout.setTitle("加载异常");
//                        queding();
//                        quxiao();
                        break;
                    case 4://账户长度是否正确符合要求 用户不存在
                        Toast.makeText(NotCompletedInfoActivity.this, "账户长度不对", Toast.LENGTH_SHORT).show();
                        break;
                    case 5://判断密码的长度密码错误
                        Toast.makeText(NotCompletedInfoActivity.this, "请检查密码", Toast.LENGTH_SHORT).show();
                        break;

                    case 7://用户不正确
                        Toast.makeText(NotCompletedInfoActivity.this, "请检查用户名", Toast.LENGTH_SHORT).show();
                        break;
                    case 8://用户不正确
                        Toast.makeText(NotCompletedInfoActivity.this, "无返回结果", Toast.LENGTH_SHORT).show();
                        break;
                    case 9://用户不正确
                        Toast.makeText(NotCompletedInfoActivity.this, "时间不对", Toast.LENGTH_SHORT).show();
                        break;

                    case 10:
                        Toast.makeText(NotCompletedInfoActivity.this, "标牌编号不正确！请返回上一层", Toast.LENGTH_SHORT).show();

                        break;
                    default:
                        Toast.makeText(NotCompletedInfoActivity.this, "请检查网络服务", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

    }


    @Override
    protected void onResume() {
        super.onResume();
        LoadData();
    }

    /****
     * 获取数据
     */
    private void LoadData() {
//        String json = GETJSOM.getJson(this, "uncompletedatainfo.json");
//        PKNotCompleteInfoCode categoryBean = JSONObject.parseObject(json, PKNotCompleteInfoCode.class);
//
//        Log.e(TAG, "json=============================================" + categoryBean.getCode());
//        Log.e(TAG, "json=============================================");
//        Log.e(TAG, "json=============================================" + categoryBean.getData());
//        for (int i = 0; i < categoryBean.getData().size(); i++) {
//            listinfoentitylist.add(categoryBean.getData().get(i));
//        }
//        for (int i = 0; i < listinfoentitylist.size(); i++) {
//            if (null == listinfoentitylist.get(i).getAllmoney() || ("".equals(listinfoentitylist.get(i).getAllmoney()))) {
//            } else {
//                sss = listinfoentitylist.get(i).getAllmoney();
//            }
//
//        }
//        handler.sendEmptyMessage(0);
        new Thread(new Runnable() {
            public void run() {
                if (null == param || ("").equals(param)) {
                    handler.sendEmptyMessage(10);
                } else {
                    try {
                        Log.e(TAG, "---------------------------------");
                        String netResult = UpServer.getScutcheonDetailAndSum1(param);
                        if (null == netResult || ("").equals(netResult)) {
                            handler.sendEmptyMessage(3);
                        } else {
                            listinfoentitylist.clear();
                            Log.d(TAG, "---------------------------------" + netResult);
                            listinfoentitylist = JSONObject.parseArray(netResult, PKNotCompleteInfoEntity.class);
                            Log.e(TAG, "json==============长度===============================" + listinfoentitylist.size());

                            for (int i = 0; i < listinfoentitylist.size(); i++) {
                                if (null == listinfoentitylist.get(i).getMoneyCount()) {

                                } else {
                                    showAllmongye = listinfoentitylist.get(i).getMoneyCount();
                                    listinfoentitylist.remove(listinfoentitylist.size() - 1);
                                }
                            }
//                Log.e(TAG, "json=============================================");
//                Log.e(TAG, "json=============================================" + categoryBean.getParams()
//                );
//                for (int i = 0; i < categoryBean.getParams().size(); i++) {
//                    listinfoentitylist.add(categoryBean.getParams().get(i));
//                }
//                for (int i = 0; i < listinfoentitylist.size(); i++) {
//                    if (null == listinfoentitylist.get(i).getMoneyCount()) {
//
//                    } else {
//                        showAllmongye = listinfoentitylist.get(i).getMoneyCount();
//                    }
//                }


                            handler.sendEmptyMessage(0);
                        }

                    } catch (TimeoutException e) {
                    handler.sendEmptyMessage(2);//查无此人信息
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//查无此人信息
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//不是数字
                } catch (Exception e) {

                    e.printStackTrace();
                    Log.e("b", "异常" + e);
//                    Log.e("data报文加密失败！", e.getMessage());
                    handler.sendEmptyMessage(2);
                }


                }
            }
        }).start();
    }

    private void InitView() {
        tvcount = (TextView) findViewById(R.id.tvcount);
        acnotcomblack = (ImageView) findViewById(R.id.acnotcomblack);// 返回
        acnotcomblack.setOnClickListener(this);

//        tvcount.setText("111");
        nocompletenolistview = (ListView) findViewById(R.id.nocompletenolistview);
        BiaoCaidNo = (TextView) findViewById(R.id.nocompletebiaopaino);
        BiaoCaidNo.setText(param);//  标牌
        nocompletetaskno = (TextView) findViewById(R.id.nocompletetaskno);
        nocompletetaskno.setText(Taskno);//  任务
//        nocompletebiaopaino= (TextView) findViewById(R.id.nocompletebiaopaino);
//        PKNotCompleteInfoEntity entity = new PKNotCompleteInfoEntity();
//        entity.setPKNotInfoAmount("100");
//        entity.setPKNotInfoCompletedamage("100");
//        entity.setPKNotInfoCoupontype("5套");
//        entity.setPKNotInfoVersion("232323");
//        PKNotCompleteInfoEntity entity1 = new PKNotCompleteInfoEntity();
//        entity1.setPKNotInfoAmount("100");
//        entity1.setPKNotInfoCompletedamage("100");
//        entity1.setPKNotInfoCoupontype("5套");
//        entity1.setPKNotInfoVersion("232323");
//        listinfoentitylist.add(entity);
//        listinfoentitylist.add(entity1);
//        listinfoentitylist.clear();

        user = (TextView) findViewById(R.id.nocompletelogininfo);
        user.setText(app.user.getName());
        hbt_ql_ruku_update = (Button) findViewById(R.id.hbt_ql_ruku_update);
        hbt_ql_ruku_update.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.acnotcomblack:
                NotCompletedInfoActivity.this.finish();
                break;
            case R.id.hbt_ql_ruku_update:
                LoadData();
                break;
            default:
                break;
        }

    }
}
